﻿using Newtonsoft.Json;
using PMSWenLvTing.Model;
using PMSWenLvTing.Model.JiuDian;
using PMSWenLvTing.Model.WenLvTing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace PMSWenLvTing.Utility
{
    /// <summary>
    /// 将原始数据转换成dto
    /// </summary>
    public static class ToDTO
    {
        /// <summary>
        /// jiudian->DTO_JDShort
        /// </summary>
        /// <param name="lisjd"></param>
        /// <returns></returns>
        public static List<DTO_JDShort> Self_JDToJDShort(List<jiudian> lisjd)
        {
            List<DTO_JDShort> lisres = new List<DTO_JDShort>();
            if (lisjd == null || lisjd.Count <= 0)
            {
                return lisres;
            }
            foreach (jiudian item in lisjd)
            {
                DTO_JDShort model = new DTO_JDShort();
                model.id = item.id;
                model.jd_name = item.jd_name;
                model.isselected = 0;

                lisres.Add(model);
            }
            return lisres;
        }

        /// <summary>
        /// 将报表综合数据转换成报表展示数据
        /// </summary>
        /// <param name="lisjd">酒店数据</param>
        /// <param name="lisbbfb">报表发布数据</param>
        /// <param name="lisbbzt">报表状态数据</param>
        /// <param name="lisyhjd">用户酒店数据</param>
        /// <returns></returns>
        public static List<DTO_BBZTShow> Self_ToBBShow(List<jiudian> lisjd, List<baobiao_fabu> lisbbfb, List<baobiao_zhuangtai> lisbbzt, List<yonghu_jiudian> lisyhjd)
        {
            List<DTO_BBZTShow> lisres = new List<DTO_BBZTShow>();
            if (lisbbzt == null || lisbbzt.Count <= 0)
            {
                return lisres;
            }
            jiudian jdmodel = new jiudian();
            baobiao_fabu fbmodel = new baobiao_fabu();
            yonghu_jiudian yhmodel = new yonghu_jiudian();
            DateTime dt2000 = new DateTime(2000, 1, 1);
            foreach (baobiao_zhuangtai zt in lisbbzt)
            {
                if (fbmodel == null)
                {
                    continue;
                }
                jdmodel = lisjd.Where(kkm => kkm.id == zt.jdid).FirstOrDefault();
                fbmodel = lisbbfb.Where(kkm => kkm.id == zt.bbid).FirstOrDefault();
                yhmodel = lisyhjd.Where(kkm => kkm.id == zt.yyjdid).FirstOrDefault();
                DTO_BBZTShow model = new DTO_BBZTShow();
                model.bbztid = zt.id;
                model.bb_fabu_dt = (fbmodel.fabu_shijian == dt2000 ? "" : fbmodel.fabu_shijian.ToString("yyyy-MM-dd"));
                model.bb_kaishi_dt= (fbmodel.tianbao_kaishi == dt2000 ? "" : fbmodel.tianbao_kaishi.ToString("yyyy-MM-dd"));
                model.bb_leixing = fbmodel.leixing;
                model.bb_leixing_str = CommonHelper.Self_BBLeiXingToStr(fbmodel.leixing);
                model.bb_name = fbmodel.bb_name;
                model.bb_riqi= (fbmodel.bb_riqi == dt2000 ? "" : CommonHelper.Self_BBRiQiToStr(fbmodel.leixing, fbmodel.bb_riqi));
                model.bb_tianbao_dt = CommonHelper.Self_BBTianBaoRiQiToStr(zt.zt_tianbao, zt.dt_tianbao);
                model.jdid = zt.jdid;
                model.jdyhid = zt.yyjdid;
                model.jdyh_name = yhmodel == null ? "" : yhmodel.xingming;
                model.jd_name = jdmodel.jd_name;
                model.bb_jieshu_dt = fbmodel.tianbao_jieshu == dt2000 ? "" : fbmodel.tianbao_jieshu.ToString("yyyy-MM-dd");
                model.bb_shenhe_zt = zt.zt_shenhe;
                model.bb_shenhe_zt_str = CommonHelper.Self_BBShenHeStateToStr(zt.zt_shenhe);

                lisres.Add(model);
            }
            return lisres;
        }

        /// <summary>
        /// 将酒店用户数据转换成可以展示的数据
        /// </summary>
        /// <param name="lisjd">当前主账户下的酒店数据</param>
        /// <param name="lisyhjd">酒店用户数据</param>
        /// <returns></returns>
        public static List<DTO_yonghu_jiudian> Self_ToYHJDShow(List<jiudian> lisjd, List<yonghu_jiudian> lisyhjd)
        {
            List<DTO_yonghu_jiudian> lisres = new List<Model.JiuDian.DTO_yonghu_jiudian>();
            if (lisyhjd == null || lisyhjd.Count <= 0)
            {
                return lisres;
            }
            foreach (yonghu_jiudian yh in lisyhjd)
            {
                DTO_yonghu_jiudian model = new DTO_yonghu_jiudian();
                model.guid_jdyh = yh.guid_jdyh;
                model.id = yh.id;
                model.jdid_json = yh.jdid_json;
                model.leixing = yh.leixing;
                model.leixing_str = CommonHelper.Self_JDYHLeiXingToStr(yh.leixing);
                #region 获取酒店名称
                List<int> lisjdid = JsonConvert.DeserializeObject<List<int>>(yh.jdid_json);
                model.lisjd = Self_JDToJDShort(lisjd);
                foreach (DTO_JDShort item in model.lisjd)
                {
                    if (lisjdid.Exists(kkm => kkm == item.id))
                    {
                        item.isselected = 1; 
                    }
                }
                
                #endregion
                model.xingming = yh.xingming;
                model.zhanghao = yh.zhanghao;
                model.zhuangtai = yh.zhuangtai;
                model.zhuangtai_str = CommonHelper.Self_JDYHZhuangTaiToStr(yh.zhuangtai);
                model.mother_id = yh.mother_id;
                model.lisjdid = lisjdid;

                lisres.Add(model);
            }
            return lisres;
        }

        /// <summary>
        /// 将数据转换成消息model
        /// </summary>
        /// <param name="lisjd">酒店数据</param>
        /// <param name="lisbbfb">报表发布数据</param>
        /// <param name="lisbbzt">报表状态数据</param>
        /// <returns></returns>
        public static List<DTO_JDMsg> Self_ToJDMsg(List<jiudian> lisjd, List<baobiao_fabu> lisbbfb, List<baobiao_zhuangtai> lisbbzt)
        {
            List<DTO_JDMsg> lisres = new List<DTO_JDMsg>();
            if (lisbbzt == null || lisbbzt.Count <= 0)
            {
                return lisres;
            }
            baobiao_fabu modelfb = new baobiao_fabu();
            jiudian modeljd = new jiudian();
            foreach (baobiao_zhuangtai zt in lisbbzt)
            {
                modelfb = lisbbfb.Where(kkm => kkm.id == zt.bbid).FirstOrDefault();
                modeljd = lisjd.Where(kkm => kkm.id == zt.jdid).FirstOrDefault();
                DTO_JDMsg model = new DTO_JDMsg();
                model.bbid = zt.bbid;
                model.bbztid = zt.id;
                model.bb_name = modelfb == null ? "" : modelfb.bb_name;
                model.dt_str = modelfb == null ? "" : modelfb.fabu_shijian.ToString("yyyy-MM-dd");
                model.jdid = zt.jdid;
                model.jd_name = modeljd == null ? "" : modeljd.jd_name;

                lisres.Add(model);
            }
            return lisres;
        }

        /// <summary>
        /// 将数据转换成文旅厅消息model
        /// </summary>
        /// <param name="lisjd">酒店数据</param>
        /// <param name="lisbbfb">报表发布数据</param>
        /// <param name="lisbbzt">报表状态数据</param>
        /// <returns></returns>
        public static List<DTO_WLTMsg> Self_BBToWLTMsg(List<jiudian> lisjd, List<baobiao_fabu> lisbbfb, List<baobiao_zhuangtai> lisbbzt)
        {
            List<DTO_WLTMsg> lisres = new List<DTO_WLTMsg>();
            if (lisbbzt == null || lisbbzt.Count <= 0)
            {
                return lisres;
            }
            baobiao_fabu modelfb = new baobiao_fabu();
            jiudian modeljd = new jiudian();
            foreach (baobiao_zhuangtai zt in lisbbzt)
            {
                modelfb = lisbbfb.Where(kkm => kkm.id == zt.bbid).FirstOrDefault();
                modeljd = lisjd.Where(kkm => kkm.id == zt.jdid).FirstOrDefault();
                DTO_WLTMsg model = new DTO_WLTMsg();
                model.bbid = zt.bbid;
                model.bbztid = zt.id;
                model.bb_name = modelfb == null ? "" : modelfb.bb_name;
                model.dt_str = modelfb == null ? "" : modelfb.fabu_shijian.ToString("yyyy-MM-dd");
                model.jdid = zt.jdid;
                model.jd_name = modeljd == null ? "" : modeljd.jd_name;
                model.msg_lx = 2;

                lisres.Add(model);
            }
            return lisres;
        }

        /// <summary>
        /// 将数据转换成文旅厅消息model
        /// </summary>
        /// <param name="lisjd">酒店数据</param>
        /// <param name="lisbbfb">报表发布数据</param>
        /// <param name="lisbbzt">报表状态数据</param>
        /// <returns></returns>
        public static List<DTO_WLTMsg> Self_JDToWLTMsg(List<jiudian> lisjd)
        {
            List<DTO_WLTMsg> lisres = new List<DTO_WLTMsg>();
            if (lisjd == null || lisjd.Count <= 0)
            {
                return lisres;
            }
            foreach (jiudian jd in lisjd)
            {
                DTO_WLTMsg model = new DTO_WLTMsg();
                model.bbid = -1;
                model.bbztid = jd.id;
                model.bb_name = "";
                model.dt_str = jd.shenqing_shijian.ToString("yyyy-MM-dd");
                model.jdid = jd.id;
                model.jd_name =jd.jd_name;
                model.msg_lx = 1;

                lisres.Add(model);
            }
            return lisres;
        }

        /// <summary>
        /// 将酒店数据转换成文旅厅端展示的数据
        /// </summary>
        /// <param name="lisjd">酒店数据</param>
        /// <param name="lisarea">区域数据</param>
        /// <returns></returns>
        public static List<DTO_JDShow_WLT> Self_ToJDShowWLT(List<jiudian> lisjd, List<t_area> lisarea)
        {
            List<DTO_JDShow_WLT> lisRes = new List<DTO_JDShow_WLT>();
            if (lisjd == null || lisjd.Count <= 0)
            {
                return lisRes;
            }
            t_area modelArea = new t_area();
            foreach (jiudian jd in lisjd)
            {
                modelArea = lisarea.Where(kkm => kkm.areaId == jd.aid).FirstOrDefault();
                DTO_JDShow_WLT model = new DTO_JDShow_WLT();
                model.areaName = modelArea == null ? "" : modelArea.areaName;
                model.dianhua = jd.dianhua;
                model.jdid = jd.id;
                model.jd_name = jd.jd_name;
                model.jingying_leixing_huafen = jd.jingying_leixing_huafen;
                lisRes.Add(model);
            }
            return lisRes;
        }

        /// <summary>
        /// 将报表综合数据转换成报表展示数据
        /// </summary>
        /// <param name="lisjd">酒店数据</param>
        /// <param name="lisbbfb">报表发布数据</param>
        /// <param name="lisbbzt">报表状态数据</param>
        /// <param name="lisyhjd">用户酒店数据</param>
        /// <param name="lisyhwlt">用户文旅厅数据</param>
        /// <returns></returns>
        public static List<DTO_ZTShow_WLT> Self_ToZTShow_WLT(List<jiudian> lisjd, List<baobiao_fabu> lisbbfb, List<baobiao_zhuangtai> lisbbzt, List<yonghu_jiudian> lisyhjd,List<yonghu_wenlvting> lisyhwlt)
        {
            List<DTO_ZTShow_WLT> lisres = new List<DTO_ZTShow_WLT>();
            if (lisbbzt == null || lisbbzt.Count <= 0)
            {
                return lisres;
            }
            jiudian jdmodel = new jiudian();
            baobiao_fabu fbmodel = new baobiao_fabu();
            yonghu_jiudian yhmodel = new yonghu_jiudian();
            yonghu_wenlvting yhwltmodel = new yonghu_wenlvting();
            DateTime dt2000 = new DateTime(2000, 1, 1);
            foreach (baobiao_zhuangtai zt in lisbbzt)
            {
                if (fbmodel == null)
                {
                    continue;
                }
                jdmodel = lisjd.Where(kkm => kkm.id == zt.jdid).FirstOrDefault();
                fbmodel = lisbbfb.Where(kkm => kkm.id == zt.bbid).FirstOrDefault();
                yhmodel = lisyhjd.Where(kkm => kkm.id == zt.yyjdid).FirstOrDefault();
                yhwltmodel = lisyhwlt.Where(kkm => kkm.id == zt.yywltid).FirstOrDefault();
                DTO_ZTShow_WLT model = new DTO_ZTShow_WLT();
                model.bbztid = zt.id;
                model.dt_fabu = (fbmodel.fabu_shijian == dt2000 ? "" : fbmodel.fabu_shijian.ToString("yyyy-MM-dd"));
                model.dt_kaishi = (fbmodel.tianbao_kaishi == dt2000 ? "" : fbmodel.tianbao_kaishi.ToString("yyyy-MM-dd"));
                model.bb_leixing = fbmodel.leixing;
                model.bb_leixing_str = CommonHelper.Self_BBLeiXingToStr(fbmodel.leixing);
                model.bbname = fbmodel.bb_name;
                model.bb_riqi = (fbmodel.bb_riqi == dt2000 ? "" : CommonHelper.Self_BBRiQiToStr(fbmodel.leixing, fbmodel.bb_riqi));
                model.dt_tianbao = CommonHelper.Self_BBTianBaoRiQiToStr(zt.zt_tianbao, zt.dt_tianbao);
                model.jdid = zt.jdid;
                model.jdyhid = zt.yyjdid;
                model.jdyh_name = yhmodel == null ? "" : yhmodel.xingming;
                model.jd_name = jdmodel.jd_name;
                model.dt_jiezhi = fbmodel.tianbao_jieshu == dt2000 ? "" : fbmodel.tianbao_jieshu.ToString("yyyy-MM-dd");
                model.zt_shenhe = zt.zt_shenhe;
                model.zt_shenhe_str = CommonHelper.Self_BBShenHeStateToStr(zt.zt_shenhe);
                model.yywltid = zt.yywltid;
                model.yywltid_name = yhwltmodel == null ? "" : yhwltmodel.xingming;
                model.zt_yunying_str = jdmodel.jingying_zhuangtai;

                lisres.Add(model);
            }
            return lisres;
        }

        /// <summary>
        /// 将文旅厅用户信息转换成可以展示的model
        /// </summary>
        /// <param name="lisyh">文旅厅用户数据</param>
        /// <param name="lisarea">区域数据,当前海南区即可</param>
        /// <returns>文旅厅用户展示数据</returns>
        public static List<DTO_yonghu_wenlvting> Self_ToYHWLTShow(List<yonghu_wenlvting> lisyh, List<t_area> lisarea)
        {
            List<DTO_yonghu_wenlvting> lisRes = new List<DTO_yonghu_wenlvting>();
            if (lisyh == null || lisyh.Count <= 0)
            {
                return lisRes;
            }
            t_area modelarea = new t_area();
            StringBuilder sb = new StringBuilder();
            foreach (yonghu_wenlvting item in lisyh)
            {
                DTO_yonghu_wenlvting model = new DTO_yonghu_wenlvting();
                model.aid_json = JsonConvert.DeserializeObject<List<int>>(item.aid_json);
                #region aid_json_str
                sb.Clear();
                foreach (int aid in model.aid_json)
                {
                    modelarea = lisarea.Where(kkm => kkm.areaId == aid).FirstOrDefault();
                    sb.Append(modelarea == null ? "" : modelarea.areaName);
                    sb.Append(",");
                }
                model.aid_json_str = sb.ToString();
                #endregion
                model.id = item.id;
                model.leixing = item.leixing;
                model.leixing_str = CommonHelper.Self_WLTYHLeiXingToStr(item.leixing);
                model.quanxian = item.quanxian;
                model.quanxian_str = CommonHelper.Self_WLTYHQuanXianToStr(item.quanxian);
                model.xingming = item.xingming;
                model.zhanghao = item.zhanghao;
                model.zhuangtai = item.zhuangtai;
                model.zhuangtai_str = CommonHelper.Self_WLTYHZhuangTaiToStr(item.zhuangtai);

                lisRes.Add(model);
            }

            return lisRes;
        }

        /// <summary>
        /// 将报表发布数据转换成可以展示的数据
        /// </summary>
        /// <param name="lisbbfb">报表发布数据</param>
        /// <returns></returns>
        public static List<DTO_BBShow_WLT> Self_ToBBShowWLT(List<baobiao_fabu> lisbbfb)
        {
            List<DTO_BBShow_WLT> lisRes = new List<DTO_BBShow_WLT>();
            if (lisbbfb == null)
            {
                return lisRes;
            }
            foreach (baobiao_fabu item in lisbbfb)
            {
                DTO_BBShow_WLT model = new DTO_BBShow_WLT();
                model.bb_name = item.bb_name;
                model.bb_riqi_str = CommonHelper.Self_BBRiQiToStr(item.leixing, item.bb_riqi);
                model.fabu_ren = item.fabu_ren;
                model.fabu_shijian_str = CommonHelper.Self_RiQiToStr2000(item.fabu_shijian, true);
                model.id = item.id;
                model.is_duanxin_str = item.is_duanxin ? "发送短信" : "不发送短信";
                model.leixing = item.leixing;
                model.leixing_str = CommonHelper.Self_BBLeiXingToStr(item.leixing);
                model.tianbao_jieshu_str= CommonHelper.Self_RiQiToStr2000(item.tianbao_jieshu, false);
                model.tianbao_kaishi_str = CommonHelper.Self_RiQiToStr2000(item.tianbao_kaishi, false);
                model.yywltid = item.yywltid;

                lisRes.Add(model);

            }
            return lisRes;
        }


    }
}